package domain.command;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.command.CommandError;
import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.command.impl.Command;
import org.dsrg.soenea.domain.helper.Helper;
import org.dsrg.soenea.uow.MissingMappingException;
import org.dsrg.soenea.uow.UoW;

import domain.model.group.Group;
import domain.model.group.GroupProxy;
import domain.model.group.IGroup;
import domain.model.group.mapper.GroupInputMapper;
import domain.model.invite.Invite;
import domain.model.invite.mapper.InviteInputMapper;
import domain.model.requestInvite.RequestInvite;
import domain.model.requestInvite.mapper.RequestInviteInputMapper;
import domain.model.user.User;
import domain.model.user.mapper.UserInputMapper;

public class RemoveGroupCommand extends Command
{

	private boolean post;

	public RemoveGroupCommand(Helper helper, boolean ispost) {
		super(helper);
		post = ispost;
	}

	public void setUp() throws CommandException 
	{
	}

	public void process() throws CommandException 
	{
		User remover = null;
		long uid = -1, gid = -1, gver= -1;
		List<User> userList = null;
		List<User> memberList = new ArrayList<User>();

		try
		{

			//First getting the user and group info 
			uid = (Long)helper.getSessionAttribute("userid");
			remover = UserInputMapper.find(uid);
			gid = remover.getGroup().getId();
			gver = remover.getGroup().getVersion();


			//getting all users
			userList = UserInputMapper.findAll();

			//adding only the people in the same group to a member list
			for(User u: userList)
			{
				if(u.getGroup().getId() == gid)
				{
					memberList.add(u);
				}
			}	

			// if "POST" then try to remove all users and the group.
			if (post) 
			{
				removeGroup(memberList, gid);
			}


			// set attributes to pass to the view
			helper.setRequestAttribute("groupId", gid);
			helper.setRequestAttribute("groupVer", gver);
			helper.setRequestAttribute("memberList", memberList);

		}
		catch (Exception e) 
		{		
			e.printStackTrace();
			throw new CommandException(e.getMessage());			
		}

	}

	public void tearDown() throws CommandError 
	{
	}

	
	//This method could be removed if we add these steps into the GroupOutputMapper's cascade delete section perhaps?
	private void removeGroup(List<User> memberList,long gid) throws SQLException, MissingMappingException, MapperException, InstantiationException, IllegalAccessException
	{

		//first removing all members from the group
		for(User u: memberList)
		{
			//setting the group to 0   ** Might be a better way to do this...
			IGroup gr = new GroupProxy((long) 0);
			u.setGroup(gr);
			UoW.getCurrent().registerDirty(u);
		}

		//Next removing all invites to the group
		List<Invite> inviteList = InviteInputMapper.findAll();
		
		//List<Invite> memberInviteList = new ArrayList<Invite>();
		
		
		//adding only invites to this particular group
		for(Invite inv: inviteList)
		{
			if(inv.getGroup().getId() == gid)
			{
				UoW.getCurrent().registerDirty(inv);
			}
		}
		
		//Next removing all request invites to the group
		List<RequestInvite> reqInviteList = RequestInviteInputMapper.findAll();	
		
		//adding only reqinvites to this particular group
				for(RequestInvite req: reqInviteList)
				{
					if(req.getGroup().getId() == gid)
					{
						UoW.getCurrent().registerDirty(req);
					}
				}
		
		
		
		//Finally deleting group
		Group gr = GroupInputMapper.find(gid);
		UoW.getCurrent().registerRemoved(gr);
		
		//commiting all changes to DB
		UoW.getCurrent().commit();

	}
}
